package edu.mtsu.csci3033;

import java.awt.*;
import java.util.Random;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;


/** Description of TeamProject Table class
 *
 * @author Dan Woodson
 * @author Trevor Powis
 * @author Kristi Brockelsby
 * @author Jared Gailey
 * @version 1: October 26, 2009
 */
public class Grid
{
	private char[][]values = new char[10][10];	//probably better to use array of integer values(0 or 1)
	Image bugImg; // stores the bug image
	private int Rows;	// stores number of rows
	private int Columns;	// stores number of columns
	private int BoxWidth ;	// stores the width of a box
	private int BoxHeight;	// stores the height of a box
	public Point Pos = new Point();	// creates a new point to draw from

	private int tableHeight;	// stores the height of the table
	private int tableWidth;		// stores the width of the table
 	 /** Description of public Grid()
  	*
  	* Constructor function
	* @param itemImg contains the "target" image ie: ant, fly, unicorn
	* @return	none
  	*/
	public Grid()
	{
		Rows = 15;
		Columns = 15;
		BoxWidth = 25;
		BoxHeight = 25;
		Pos.x = 100;
		Pos.y = 100;
		//Image bugImg = getImage("images/fly.gif");
		tableHeight = Rows*BoxHeight;
		tableWidth = Columns*BoxWidth;
	}


 	/** Description of draw(Graphics g)
  	*
	* Function draws the lines used in the table
	* @param g	object used to draw the table
	* @return	none
  	*/
	public void draw(Graphics g)
	{
		//draw vertical table lines
		for (int i = 0 ; i <= Columns ; i++)
			g.drawLine(i*BoxWidth + Pos.x, Pos.y, i*BoxWidth + Pos.x, tableHeight+Pos.y);

		//draw horizontal table line
		for(int i = 0 ; i <= Rows ; i++)
			g.drawLine(Pos.x, i*BoxHeight + Pos.y, tableWidth+Pos.x, i*BoxHeight + Pos.y);
		
		this.drawLables(g);
	}
 	/** Description of drawLables(Graphics g)
  	* 
	* Function draws the Lables of the Table
	* @param g	object used to draw the table
	* @return	none
  	*/	
	private void drawLables(Graphics g)
  	{
  		String Lable;
  		Graphics2D g2 = (Graphics2D)g;
        
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        	RenderingHints.VALUE_ANTIALIAS_ON);
        Font font = new Font("Serif", Font.PLAIN, 10);
        g2.setFont(font);
        
        
        int offSetX = BoxWidth/2 - 9;
        int offSetY = BoxHeight/2 - 12;
        
        //draw Row lables
        for (int i = 0 ; i <= Rows ; i++)
        {
        	Lable = "" + i;
        	 g2.drawString(Lable, Pos.x - 30, Pos.y - offSetY + BoxHeight*i);
        }
        
        //draw Column lables
        for (int i = 0 ; i <= Columns ; i++)
        {
        	Lable = "" + i;
        	 g2.drawString(Lable, Pos.x - offSetX + BoxWidth*i, Pos.y - 15 );
        }
  	}



	/** Description of randomChangeFunc()
	 *
	 * Function randomly determines which table value to change
	 * @param 	none
	 * @return	void
	 */
	public void randomChangeFunc()
	{

	  	//get random row and column
	  	Random rand=new Random();

    		int randRow = rand.nextInt(Rows);    // gets and holds a random column
    		int randCol = rand.nextInt(Columns); // gets and holds a random column

    		System.out.println("randRow = " + randRow + " randCol = " + randCol);

		if(values[randRow][randCol] == 'X')
			values[randRow][randCol] = 'O';
		else if(values[randRow][randCol] == 'O')
			values[randRow][randCol] = 'X';
		else
			System.out.println("ERROR SWAPPING SQUARE VALUE");  // error message

    		//set globals
    		//changedRow = randRow;
   		//changedCol = randCol;

 	 }

	public int getX(int col)
	{
		int x;
		x = col*BoxWidth + Pos.x;
		return x;
	}
	
	public int getY(int row)
	{
		int y;
		y = row*BoxHeight + Pos.y;
		return y;
	}
	
	/** Description of getColumn(int x)
  	* 
	* Function returns column number given a position on X-axis
	* @return	integer
  	*/	
	private int getColumn(int x)
	{
		int offsetx=0;
		for (int i = 0 ; i < Columns ; i++) {
			offsetx = i*BoxWidth;
			if((x>Pos.x+offsetx)&& (x<Pos.x+offsetx+BoxWidth))
				return i+1;
		}
		return -1;
	}

	/** Description of getRow(int y)
  	* 
	* Function returns Row number given a position on Y-axis
	* @return	integer
  	*/	
	private int getRow(int y)
	{
		int offsety=0;
		for (int i = 0 ; i < Rows ; i++)
		{
			offsety = i*BoxHeight;
			if((y>Pos.y+offsety)&& (y<Pos.x+offsety+BoxHeight))
				return i+1;
		}

		return -1;
	}

	

}
